package com.sengled.cloud.biz.union.mgt.dao.sys;


import com.sengled.cloud.biz.union.mgt.dao.sys.provider.RoleModulePermissionDaoProvider;
import com.sengled.cloud.biz.union.mgt.model.po.RoleModulePermission;
import com.sengled.cloud.biz.union.mgt.model.vo.ModulePermissionByRoles;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleModulePermissionDao {
    int deleteByPrimaryKey(Long id);

    @Delete("delete FROM role_module_permission WHERE role_id=#{id}")
    int deleteByRoleKey(Long id);

    @Insert("INSERT INTO role_module_permission(role_id,module_id,permission_id,create_date) " +
            "VALUES(#{roleId},#{moduleId},#{permissionId},#{createDate})")
    int insert(RoleModulePermission record);

    @InsertProvider(type = RoleModulePermissionDaoProvider.class, method = "batchInsert")
    int batchInsert(@Param("list") List<RoleModulePermission> list);

    int insertSelective(RoleModulePermission record);

    RoleModulePermission selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(RoleModulePermission record);

    int updateByPrimaryKey(RoleModulePermission record);

    @Select("select permission_id from role_module_permission where role_id = #{roleId} and module_id = #{moduleId}")
    List<Long> selectByRoleAndModuleKey(@Param("roleId") Long roleId,@Param("moduleId") Long moduleId);

    @Select("select role_id as roleId,module_id as moduleId,permission_id as permissionId from role_module_permission where role_id = #{roleId}")
    List<RoleModulePermission> selectByRoleKey(@Param("roleId") Long roleId);

    @Select("<script>"
                         + "SELECT role_id as roleId,module_id as moduleId,permission_id as permissionId FROM role_module_permission WHERE role_id IN "
                          + "<foreach item='item' index='index' collection='roleIds' open='(' separator=',' close=')'>"
                              + "#{item}"
                          + "</foreach>"
            + "</script>")
    List<RoleModulePermission> selectByRolesKey(@Param("roleIds") List<Long> roleIds);

    @Select("<script>"
                         + "SELECT DISTINCT m.id as moduleId,m.module_code as moduleCode,m.module_name as moduleName,p.permission_code as permissionCode,p.id as permissionId FROM role_module_permission rmp,module m,permission p WHERE m.id = rmp.module_id AND p.id = rmp.permission_id AND m.channel = #{channel} and rmp.role_id IN "
                          + "<foreach item='item' index='index' collection='roleIds' open='(' separator=',' close=')'>"
                              + "#{item}"
                          + "</foreach>"
            + "</script>")
    List<ModulePermissionByRoles> selectLoginInfoByRolesKey(@Param("roleIds") List<Long> roleIds, @Param("channel") String channel);

    @Update("update role_module_permission set enable_mark = #{enableMark} where role_id = #{roleId} ")
    int updateEnableMarkByRoleId(@Param("roleId") Long roleId,@Param("enableMark") Integer enableMark);
}